/**
 * 
 */
package com.xbang.attendance.web.action.warehouse.addproduct;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;

import com.xbang.attendance.entity.stock.InventoryRecord;
import com.xbang.attendance.service.InventoryService;
import com.xbang.attendance.service.ProductService;
import com.xbang.attendance.service.SystemService;
import com.demo2do.lighturl.annotation.Action;
import com.xbang.core.dao.search.component.SearchComponent;
import com.xbang.core.page.Result;
import com.xbang.core.struts.support.SearchActionSupport;

import enumeration.InventoryOperation;

/**
 * @author dev.zhang
 */
public class Index extends SearchActionSupport {

	private static final long serialVersionUID = 2886204759816132683L;

	@Autowired
	private SystemService systemService;

	@Autowired
	private ProductService productService;
		
	@Autowired
	private InventoryService inventoryService;
	
	
	private Integer id;
	private String ids;
	private String soNumber;

	private List<InventoryRecord> inventoryRecords = new ArrayList<InventoryRecord>();
	
	@Action("/warehouse/addproduct/index/${currPageId}")
	public String listByPage() throws Exception {
	//	System.out.println(currPageId);
		return execute();
	}
	/*
	 * (non-Javadoc)
	 * 
	 * @see com.opensymphony.xwork2.ActionSupport#execute()
	 */
	@Override
	@Action("/warehouse/addproduct/list")
	public String execute() throws Exception {
		String hqlSentence = "FROM InventoryRecord inv WHERE (inv.lastInventoryOperation = '"+InventoryOperation.OUT +"' OR inv.lastInventoryOperation = '"+InventoryOperation.TRANSFERRED+"')";
		if (searchComponent.getGroupSentences().isEmpty()){
			//searchComponent.addSearchElement("groupBy.inv.purchaseOrder.id","");
			//SElECT DISTINCT inv.purchaseOrder.id 
			searchComponent.addSearchElement("groupBy.inv.purchaseOrder.id","");
		}
		//inventoryRecords = systemService.searchForList(hqlSentence, new HashMap());
		
		if (page != null) {
			if (currPageId.intValue() != -1 && currPageId.intValue() > 0) {
				page.setCurrentPage(currPageId);
			}
		}
		
		Result result = systemService.searchByPage(hqlSentence, searchComponent, page);

		inventoryRecords = result.getItems();
		
		for (InventoryRecord inventoryRecord : inventoryRecords) {
			if(inventoryService.isAddProduct(inventoryRecord.getPurchaseOrder().getId())){
				inventoryRecord.setAddProduct(true);
			}else{
				inventoryRecord.setAddProduct(false);
			}
			
		}	
		
		page = result.getPage();

		//logger.info(">>>>>>>>>>>>");
		return "index";
	}

	@Override
	public void resetCustomerSearchCriteria() {
		// TODO Auto-generated method stub

	}


	@Action("/warehouse/addproduct/list/search")
	public String detailSearch() throws Exception {
		return "search";
	}

	@Action("/warehouse/addproduct/selectaddproduct")
	public String selectAddProdct() throws Exception {
		inventoryRecords = new ArrayList<InventoryRecord>();
		String [] idss = ids.split(",");
		for (String str : idss) {
			inventoryRecords.add((InventoryRecord) systemService.get(InventoryRecord.class, Integer.parseInt(str)));
		}
		
		return "view";
	}
	@Action("/warehouse/addproduct/selectaddproduct/do")
	public String selectAddProdctDo() throws Exception {
		inventoryRecords = new ArrayList<InventoryRecord>();
		String [] idss = ids.split(",");
		for (String str : idss) {
			inventoryRecords.add((InventoryRecord) systemService.get(InventoryRecord.class, Integer.parseInt(str)));
		}
		for (InventoryRecord inventoryRecord : inventoryRecords) {
			List<InventoryRecord> list = null;
			String hqlSentence = "FROM InventoryRecord inv WHERE (inv.lastInventoryOperation = '"+InventoryOperation.OUT 
				+"' OR inv.lastInventoryOperation = '"+InventoryOperation.TRANSFERRED+"')";
			SearchComponent searchComponent = new SearchComponent();
			searchComponent.addSearchElement("eq.inv.purchaseOrder.orderNumber",inventoryRecord.getPurchaseOrder().getOrderNumber());
			list = systemService.searchForList(hqlSentence, searchComponent);
			inventoryService.addProduct(list,soNumber);
		}
		return "index";
	}

	public List<InventoryRecord> getInventoryRecords() {
		return inventoryRecords;
	}

	public void setInventoryRecords(List<InventoryRecord> inventoryRecords) {
		this.inventoryRecords = inventoryRecords;
	}

	/**
	 * @return the id
	 */
	public Integer getId() {
		return id;
	}

	/**
	 * @param id
	 *            the id to set
	 */
	public void setId(Integer id) {
		this.id = id;
	}

	public String getIds() {
		return ids;
	}

	public void setIds(String ids) {
		this.ids = ids;
	}

	public String getSoNumber() {
		return soNumber;
	}

	public void setSoNumber(String soNumber) {
		this.soNumber = soNumber;
	}
	

}
